package edu.uwm.capstone.diasmart.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.test.suitebuilder.annotation.Suppress;
import android.text.InputType;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import edu.uwm.capstone.android.util.AndroidUtil;
import edu.uwm.capstone.android.util.DialogUtil;
import edu.uwm.capstone.diasmart.EditListActivity;
import edu.uwm.capstone.diasmart.MainActivity;
import edu.uwm.capstone.diasmart.R;
import edu.uwm.capstone.diasmart.setup.WelcomeActivity;

@SuppressWarnings("unused")
public class Common {

	private static final String FIRST_BP_CHOICE = "first_bp_choice";
	private static final String FIRST_EDITABLE_ACTIVITY_LIST = "editable_activity_first_run";
	private static final String FIRST_RUN_KEY = "first_run";
	private static final String VERSION_KEY = "edu.uwm.capstone.diasmart.version";

	public static void checkEditableActivityFirstRun(Activity paramActivity) {
		SharedPreferences localSharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(paramActivity);
		if (localSharedPreferences.getBoolean("editable_activity_first_run",
				true)) {
			SharedPreferences.Editor localEditor = localSharedPreferences
					.edit();
			localEditor.putBoolean("editable_activity_first_run", false);
			localEditor.commit();
			localSharedPreferences.getBoolean("editable_activity_first_run",
					true);
			showMessageDialog(paramActivity,
					R.string.using_editable_activity_title,
					R.string.using_editable_activity_message);
		}
	}

	public static Intent getStartActivityIntent(Context paramContext) {
		String str1 = paramContext
				.getString(R.string.behavior_home_screen_preference_key);
		String str2 = paramContext
				.getString(R.string.behavior_home_screen_preference_default_value);
		SharedPreferences localSharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(paramContext);
		String str3 = localSharedPreferences.getString(str1, str2);
		if (localSharedPreferences.getBoolean("first_run", true)) {
			SharedPreferences.Editor localEditor = localSharedPreferences
					.edit();
			localEditor.putBoolean("first_run", false);
			localEditor.putString("edu.uwm.capstone.diasmart.version",
					AndroidUtil.getApplicationVersion(paramContext));
			localEditor.commit();
			localSharedPreferences.getBoolean("first_run", true);
			return new Intent(paramContext, WelcomeActivity.class);
		}
		if ("dashboard".equals(str3))
			return new Intent(paramContext, MainActivity.class);
		return new Intent(paramContext, EditListActivity.class);
	}

	@SuppressLint("NewApi")
	public static void setNumericKeyboardType(Activity paramActivity,
			EditText paramEditText, String paramString) {
		if ("preference".equals(paramString))
			paramString = PreferenceManager
					.getDefaultSharedPreferences(paramActivity)
					.getString(
							paramActivity
									.getString(R.string.behavior_numeric_keyboard_preference_key),
							"phone");
		if ("numeric".equals(paramString)) {
			paramEditText.setInputType(InputType.TYPE_CLASS_NUMBER);			
		}
		paramEditText.setInputType(InputType.TYPE_CLASS_PHONE);
	}

	public static void showBPNotice(Activity paramActivity) {
		SharedPreferences localSharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(paramActivity);
		if (localSharedPreferences.getBoolean("first_bp_choice", true)) {
			SharedPreferences.Editor localEditor = localSharedPreferences
					.edit();
			localEditor.putBoolean("first_bp_choice", false);
			localEditor.commit();
			DialogUtil.createInfoDialog(paramActivity,
					R.string.bp_notice_title, R.string.bp_notice).show();
		}
	}

	// public static void showDonatePage(Context paramContext)
	// {
	// paramContext.startActivity(new Intent("android.intent.action.VIEW",
	// Uri.parse("http://www.gexperts.com/products/ontrack/help/donate.php")));
	// }

	public static void showMessageDialog(Activity paramActivity, int paramInt1,
			int paramInt2) {
		ScrollView localScrollView = new ScrollView(paramActivity);
		TextView localTextView = new TextView(paramActivity);
		localScrollView.addView(localTextView);
		localTextView.setText(paramInt2);
		localTextView.setTextColor(-1);
		localTextView.setPadding(6, 6, 6, 6);
		new AlertDialog.Builder(paramActivity)
				.setTitle(paramInt1)
				.setView(localScrollView)
				.setPositiveButton(R.string.close,
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface paramAnonymousDialogInterface,
									int paramAnonymousInt) {
							}
						}).show();
	}

	/*
	 * public static void showUpgradeDialog(Activity paramActivity) { String
	 * str1 = AndroidUtil.getApplicationVersion(paramActivity);
	 * SharedPreferences localSharedPreferences =
	 * PreferenceManager.getDefaultSharedPreferences(paramActivity); if
	 * (StringUtil.isEqual(str1,
	 * localSharedPreferences.getString("com.gexperts.ontrack.version", null)))
	 * return; SharedPreferences.Editor localEditor =
	 * localSharedPreferences.edit();
	 * localEditor.putString("com.gexperts.ontrack.version", str1);
	 * localEditor.commit(); try { String str2 =
	 * AndroidUtil.getAndroidAsset(paramActivity, "changelog/changelog.html");
	 * View localView = LayoutInflater.from(paramActivity).inflate(2130903044,
	 * null); WebView localWebView =
	 * (WebView)localView.findViewById(2131361818);
	 * localWebView.setBackgroundColor(Color.parseColor("#ffffff"));
	 * localWebView.getSettings().setDefaultFontSize(14);
	 * localWebView.loadData(str2, "text/html", "UTF-8"); new
	 * AlertDialog.Builder
	 * (paramActivity).setTitle(2131230975).setIcon(2130837542
	 * ).setView(localView).setPositiveButton(2131230910, new
	 * DialogInterface.OnClickListener() { public void onClick(DialogInterface
	 * paramAnonymousDialogInterface, int paramAnonymousInt) {
	 * Common.showDonatePage(Common.this); } }).setNegativeButton(2131230913,
	 * new DialogInterface.OnClickListener() { public void
	 * onClick(DialogInterface paramAnonymousDialogInterface, int
	 * paramAnonymousInt) { } }).show(); return; } catch (IOException
	 * localIOException) { DebugUtil.debug(paramActivity,
	 * "Could not retrieve changelog", localIOException); } }
	 */
}
